# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/1/23
# @File   : TX10_Past2YearsPaidOvduTaxCnt_SX.py
# @E-mail : zhaochen@bbdservice.com


'''eof
name:有无不良欠缴税记录（调整）
code:TX10_Past2YearsPaidOvduTaxCnt_SX
tableName:
columnName:
groups:税务模块
dependencies:TX_SX_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import re
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(table_Name):
    '''
    获取表
    :param x:
    :return:
    '''
    try:
        ic_data = TX_SX_DSJ['data'].get(table_Name)
        if ic_data in null_type_list:
            return None
        else:
            return ic_data if isinstance(ic_data, list) and len(ic_data) > 0 else [{}]
    except:
        return None

def isfloat(x):
    '''
    判断x为float类型
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float):
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True', 'NAN', 'nan',
                     'False', '-inf', '-INF', '-INFINITY', '-infinity', 'NaN', 'Nan']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False


def formatDate(time):
    try:
        import re
        import datetime
        r1 = re.compile(u'年|月|日')
        time = r1.sub('.', time)
        try:
            date = datetime.datetime.strptime(time, '%Y.%m.%d').strftime('%Y%m%d')
        except:
            try:
                date = datetime.datetime.strptime(time, '%Y-%m-%d').strftime('%Y%m%d')
            except:
                try:
                    date = datetime.datetime.strptime(time, '%Y/%m/%d').strftime('%Y%m%d')
                except:
                    try:
                        r = re.compile(r'\D+')
                        tmp = r.sub('', time)
                        if len(tmp) > 8:
                            tmp = tmp[:8]
                        date = datetime.datetime.strptime(tmp, '%Y%m%d').strftime('%Y%m%d')
                    except:
                        date = None
        return date
    except:
        return None



def TX10_Past2YearsPaidOvduTaxCnt():
    try:
        syptQlqs = getTable('syptQlqs')
        if syptQlqs is None:
            return u'缺失值'
        elif syptQlqs == [{}]:
            return 0
        else:
            count = set()
            flag = []
            for i in syptQlqs:
                SE = i.get('SE')
                RQ = i.get('SSSQZ')
                #SSSQZ的时间格式为 ‘2019-03-14 00:00’
                if isfloat(SE) and formatDate(RQ) is not None:
                    RQ_tmp = datetime.datetime.strptime(formatDate(RQ), '%Y%m%d')
                    if (datetime.datetime.now() - RQ_tmp).days / 360.0 <= 2.0 and float(SE) > 1000.0:
                        count.add(str(RQ)+str(SE))
                    else:
                        pass
                else:
                    flag.append(None)
            if len(flag) == len(syptQlqs):
                return u'缺失值'
            else:
                return len(count)
    except:
        return u'缺失值'

result = TX10_Past2YearsPaidOvduTaxCnt()